package lumis.service.pagepersonalization.bean;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.portal.transaction.PortalTransactionFactory;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * Represents a widget registration.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@Entity
@Table(name = "lum_PPWidget")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@NamedQueries
(
	{
		@NamedQuery(name = Widget.FIND_BY_INTERFACE_INSTANCE_ID, query = "SELECT w from Widget w, ContentVersion cv, ContentLocale cl WHERE cv.itemId = w.id AND cl.id = cv.contentLocale.id AND cv.id = cl.publishedVersion.id AND w.interfaceInstanceId = :ii")
	}
)
public class Widget implements Serializable
{
	@Transient
	static final String FIND_BY_INTERFACE_INSTANCE_ID = "findWidgetByII";

	@Transient
	private static final long serialVersionUID = 433163915993408765L;

	@Id
	@GeneratedValue(generator = "portal-guid")
	@GenericGenerator(name = "portal-guid", strategy = "lumis.portal.dao.hibernate.GuidGenerator")
	private String id;

	private String name;

	private String description;

	private String icon;

	private String interfaceInstanceId;

	@Transient
	private ServiceInterfaceInstanceConfig interfaceInstance;

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getInterfaceInstanceId()
	{
		return interfaceInstanceId;
	}

	public void setInterfaceInstanceId(String interfaceInstanceId) throws PortalException
	{
		this.interfaceInstanceId = interfaceInstanceId;
		this.interfaceInstance = ManagerFactory.getServiceInterfaceInstanceManager().get(SessionConfig.getCurrentSessionConfig(), getInterfaceInstanceId(),
				PortalTransactionFactory.getCurrentTransaction());
	}

	public ServiceInterfaceInstanceConfig getInterfaceInstance() throws PortalException
	{
		if (interfaceInstance == null)
			this.interfaceInstance = ManagerFactory.getServiceInterfaceInstanceManager().get(SessionConfig.getCurrentSessionConfig(), getInterfaceInstanceId(),
					PortalTransactionFactory.getCurrentTransaction());

		return interfaceInstance;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getDescription()
	{
		return description;
	}

	public void setIcon(String icon)
	{
		this.icon = icon;
	}

	public String getIcon()
	{
		return icon;
	}

	public static Widget getByInterfaceInstanceId(String interfaceInstanceId)
	{
		try
		{
			Widget widget = (Widget) ManagerFactory.getEntityManager().createNamedQuery(FIND_BY_INTERFACE_INSTANCE_ID)
					.setParameter("ii", interfaceInstanceId).getSingleResult();
			return widget;
		}
		catch (NoResultException e)
		{
			return null;
		}
	}
}
